const cache = {

  get(key) {
    const rawData = localStorage.getItem(key);
    try {
      const data = JSON.parse(rawData);
      return {
        value: data.value,
        expire: data.expire,
      };
    } catch(e) {
      return null;
    }
  },

  set(key, value, expire = -1) {
    try {
      const data = {
        value,
      };
      if (typeof expire === 'number' && expire > 0) {
        data.expire = expire;
      }
      localStorage.setItem(key, JSON.stringify(data));
      return true;
    } catch(e) {
      return false;
    }
  },
};

export {
  cache,
};
